<?php

namespace App\Http\Controllers\Sdk;

use App\Http\Controllers\Controller;
use App\Models\Client;
use Illuminate\Http\Request;

class ClientController extends Controller
{
    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request)
    {
        validator($request->all(), [
            'phone' => 'required',
            'province' => 'required',
            'city' => 'required',
            'area' => 'required',
            'address' => 'required',
            'name' => 'required|max:32'
        ], [
            'phone.required' => '手机号不能为空',
            'province.required' => '请选择省份',
            'city.required' => '请选择市',
            'area.required' => '请选择区',
            'address.required' => '详细地址不能为空',
            'name.max' => '客户名称过长',
            'name.required' => '客户名称不能为空'
        ])->validate();

        $clientInfo = $request->only(['name', 'phone', 'province', 'city', 'area', 'address']);
        $clientInfo['agent_id'] = $this->user->getKey();
        $client = Client::query()->updateOrCreate(['phone' => $clientInfo['phone'], 'agent_id' => $clientInfo['agent_id']], $clientInfo);

        return $this->response->created(null, $client);
    }
}
